﻿using System;
using System.Text;
using Utils.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UtilsIntegrationTests
{
	[TestClass]
	public class StringExtensionUT
	{
		[TestMethod]
		public void TestCryptDecrypt()
		{
			const string testString = "This is a very long test string";

			string cryptedString = testString.Crypt();
			string decryptedString = cryptedString.Decrypt();

			Assert.AreEqual(testString,decryptedString);
		}

		[TestMethod]
		public void TestCryptDecryptWithEmpty()
		{
			string testString = string.Empty;

			string cryptedString = testString.Crypt();
			string decryptedString = cryptedString.Decrypt();

			Assert.AreEqual(testString, decryptedString);
		}

		[TestMethod]
		public void TestCryptDecryptWithNull()
		{
			string testString = null;

			string cryptedString = testString.Crypt();
			string decryptedString = cryptedString.Decrypt();

			Assert.AreEqual(testString, decryptedString);
		}

		[TestMethod]
		public void TestCryptDecryptWithUnicode()
		{
			string testString = Encoding.Unicode.GetString(new byte[]{48,49,48,35,12,12,52,21,54,56});

			string cryptedString = testString.Crypt();
			string decryptedString = cryptedString.Decrypt();

			Assert.AreEqual(testString, decryptedString);
		}

		[TestMethod]
		public void TestNthIndexOfNormal()
		{
			const string testString = "this is a test string with a lot of t'st";

			int index = testString.NthIndexOf("t", 1);
			Assert.AreEqual(0, index);
			
			index = testString.NthIndexOf("t", 2);
			Assert.AreEqual(10, index);

			index = testString.NthIndexOf("t", 3);
			Assert.AreEqual(13, index);

			index = testString.NthIndexOf("t", 4);
			Assert.AreEqual(16, index);

			index = testString.NthIndexOf("t", 5);
			Assert.AreEqual(24, index);

			index = testString.NthIndexOf("t", 6);
			Assert.AreEqual(31, index);

			index = testString.NthIndexOf("t", 7);
			Assert.AreEqual(36, index);

			index = testString.NthIndexOf("t", 8);
			Assert.AreEqual(39, index);

			index = testString.NthIndexOf("t", int.MaxValue);
			Assert.AreEqual(-1, index);
		}

		[TestMethod]
		public void TestNthIndexOfWithoutItem()
		{
			const string testString = "aaaaaaaa";

			int index = testString.NthIndexOf("b", 1);
			Assert.AreEqual(-1, index);

			index = testString.NthIndexOf("b", 100);
			Assert.AreEqual(-1, index);
		}

		[TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestNthIndexOfBadParameter1()
		{
			const string testString = "aaaaaaaa";

			testString.NthIndexOf("a", 0);
		}
	}
}
